package com.ibook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.ibook.bo.BookBo;
import com.ibook.common.ResponseCode;
import com.ibook.common.ServerResponse;
import com.ibook.dao.BookMapper;
import com.ibook.dao.CategoriesMapper;
import com.ibook.dao.PublishingMapper;
import com.ibook.dao.TagMapper;
import com.ibook.pojo.Book;
import com.ibook.pojo.Categories;
import com.ibook.pojo.Publishing;
import com.ibook.pojo.Tag;
import com.ibook.service.ProductService;
import com.ibook.vo.BookCommentVo;
import com.ibook.vo.BookVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by heziqi on 2017/6/5.
 */
@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private CategoriesMapper categoriesMapper;

    @Autowired
    private PublishingMapper publishingMapper;

    @Autowired
    private TagMapper tagMapper;

    public ServerResponse<PageInfo> bookList(int categoriesid,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Book> bookList = bookMapper.selectByCategoriesId(categoriesid);

        List<BookVo> bookVoList = Lists.newArrayList();
        for(Book book : bookList){
            BookVo bookVo = assembleBookVo(book);
            bookVoList.add(bookVo);
        }
        PageInfo pageResult = new PageInfo(bookList);
        pageResult.setList(bookVoList);
        return ServerResponse.createBySuccess(pageResult);
    }
    private BookVo assembleBookVo(Book book){
        BookVo bookVo = new BookVo();
        Publishing publishing = publishingMapper.selectByPrimaryKey(book.getBookPublishid());
        bookVo.setBookIntroduce(book.getBookIntroduce());
        bookVo.setBookPictureurl(book.getBookPictureurl());
        bookVo.setPublishing(publishing);
        bookVo.setBookTitle(book.getBookTitle());
        return bookVo;
    }

    public ServerResponse<PageInfo> searchLsit(String bookName, String categoriesName,
                                               String authorName, String publishingName, int pageNum, int pageSize){
        if(StringUtils.isBlank(bookName) && StringUtils.isBlank(categoriesName) &&
                StringUtils.isBlank(authorName) && StringUtils.isBlank(publishingName)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Book> bookList = bookMapper.searchList(bookName,categoriesName,authorName,publishingName);
        List<BookVo> bookVoList = Lists.newArrayList();
        if (!bookList.isEmpty()) {
            for (Book book : bookList) {
                BookVo bookVo = assembleBookVo(book);
                bookVoList.add(bookVo);
            }
        }
        PageInfo pageResult = new PageInfo(bookList);
        pageResult.setList(bookVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    public ServerResponse<List<Categories>> cateList(){
        List<Categories> list = categoriesMapper.cateList();
        return ServerResponse.createBySuccess(list);
    }

    public ServerResponse<List<Tag>> tagList(int categoriesId){
        List<Tag> list = tagMapper.searchListBycateId(categoriesId);
        return ServerResponse.createBySuccess(list);
    }

    public ServerResponse<BookBo> bookDetail(int bookId){
        BookBo bookBo = bookMapper.bookDetail(bookId);
        return ServerResponse.createBySuccess(bookBo);
    }

    public ServerResponse<List<BookVo>> bookRecom(int categoriesId){
        List<Book> bookList = bookMapper.selectByCategoriesIdLim(categoriesId);
        List<BookVo> bookVoList = Lists.newArrayList();
        for(Book book : bookList) {
            BookVo bookVo = assembleBookVo(book);
            bookVoList.add(bookVo);
        }
        return ServerResponse.createBySuccess(bookVoList);
    }

    public ServerResponse raiseHots(int bookId){
        int hots = bookMapper.selectBookHots(bookId);
        if(hots == 100){
            return ServerResponse.createBySuccess();
        }
        int count = bookMapper.raiseHots(bookId);
        if(count >0){
            return ServerResponse.createBySuccessMessage("更新热度成功");
        }
        return ServerResponse.createByErrorMessage("更新热度失败");
    }

    public ServerResponse commentToBook(int userId,int bookId,String detail){
        if (StringUtils.isBlank(detail)){
            return ServerResponse.createByErrorMessage("评论为空");
        }
        int count = bookMapper.commentToBook(userId,bookId,detail);
        if (count>0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("评论失败");
    }

    public ServerResponse<PageInfo> bookComments(int bookId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<BookCommentVo> list = bookMapper.bookComments(bookId);
        if (list.isEmpty()){
            return ServerResponse.createBySuccessMessage("评论为空");
        }
        PageInfo pageResult = new PageInfo(list);
        return ServerResponse.createBySuccess(pageResult);
    }
}
